package com.library.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.library.base.BaseController;
import com.library.base.R;
import com.library.domain.LoginBody;
import com.library.domain.pojo.Admin;
import com.library.domain.pojo.Reader;
import com.library.domain.vo.LoginUser;
import com.library.domain.vo.User;
import com.library.service.AdminService;
import com.library.service.ReaderService;
import com.library.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author : 凉小天
 * @date : 2021/6/30 10:24
 */
@RestController
public class LoginController extends BaseController {

    /**Resource和Autowired的区别
     * Autowired通过Type类型注入
     * Resource通过Name类型注入
     */
    @Resource
    private AdminService adminService;
    @Resource
    private ReaderService readerService;

    /**
     * 登录界面
     */

    @PostMapping("/login")
    public R lonin(@RequestBody LoginBody loginBody){
        String username = loginBody.getUsername();
        String password = loginBody.getPassword();
        String role = loginBody.getRole();
        Object user = null;

        if (!StringUtils.isEmpty(role)){
            if ("reader".equals(role)){
                //读者登录
                QueryWrapper<Reader> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Reader::getUsername,username).eq(Reader::getPassword,password);
                user = readerService.getOne(queryWrapper);
            }else if ("admin".equals(role)){
                //管理员登录
                QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Admin::getUsername,username).eq(Admin::getPassword,password);
                user = adminService.getOne(queryWrapper);
            }
        }

        if (user != null){
            //登录成功
            String uuid = UUIDUtils.getUUID();
            Long userId = "admin".equals(role) ? ((Admin)user).getId() : ((Reader)user).getReaderId();
            LoginUser.addUser(uuid,role, userId);
            LoginUser.addVisitCount();//访问数量加一
            return result(uuid,"登陆成功");
        }else {
            return result(R.ERROR,"登录失败!账号或密码错误!");
        }

    }

    /**
     * 获取用户信息
     */
    @GetMapping("/getInfo")
    public R getInfo(String token){
        if (!StringUtils.isEmpty(token)){
            User u = LoginUser.getUserByToken(token);
            if (u != null){
                //token验证成功
                String role = u.getRole();
                Long userId = u.getUserId();
                Object user = null;

                if("reader".equals(role)){
                    //获取读者信息
                    QueryWrapper<Reader> wrapper = new QueryWrapper<>();
                    wrapper.lambda().eq(Reader::getReaderId,userId);
                    user = readerService.getOne(wrapper);
                }else if ("admin".equals(role)){
                    //获取管理员信息
                    QueryWrapper<Admin> wrapper = new QueryWrapper<>();
                    wrapper.lambda().eq(Admin::getId, userId);
                    user = adminService.getOne(wrapper);
                }
                return result(user);
            }
        }
        return result(R.SUCCESS,"登录成功");
    }
    /**
     * 获取访问量
     */
    @GetMapping("/visitCount")
    public R visitCount(){
        return result(LoginUser.getVisitCount());
    }
}
